import axios from 'axios';
import { ElMessage } from 'element-plus';

export interface ResponseCommon<T> {
  code: string;
  data: T;
  msg: string;
}

const axiosInstance = axios.create({
  timeout: 30 * 1000,
  baseURL: '/sys',
});

axiosInstance.interceptors.request.use(
  (config) => {
    console.log(config);
    return config;
  },
  (error) => Promise.reject(error),
);

axiosInstance.interceptors.response.use(
  ({ data }) => {
    if (data.code !== '0') {
      ElMessage.error(data.msg);
      return Promise.reject(data);
    }
    return data;
  },
  (error) => Promise.reject(error),
);

export default axiosInstance;
